cmake_minimum_required(VERSION 3.22)

# 打印工具链信息
message(STATUS "====================Tool Chain Info====================")
if (USE_ARMCLANG)
    include(BSP/armclang/armclang.cmake)
    message("Tool chain: armclang")
else ()
    include(BSP/armgnu/gcc-arm-none-eabi.cmake)
    message("Tool chain: arm-gnu")
endif ()


# ====== 项目配置  ======
set(CMAKE_PROJECT_NAME SheriffOS2)
project(${CMAKE_PROJECT_NAME} LANGUAGES C CXX ASM)


message("Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C++ compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS "Linker: ${CMAKE_LINKER}")
message(STATUS "C++ standard requested: ${CMAKE_CXX_STANDARD}")
message(STATUS "C++ extensions enabled: ${CMAKE_CXX_EXTENSIONS}")
message(STATUS "=======================================================")

# 添加公共宏
add_definitions(
        -DTX_INCLUDE_USER_DEFINE_FILE
        -DTX_EXECUTION_PROFILE_ENABLE
        -DTX_CORTEX_M_EPK
)

# 生成可执行文件
add_executable(${CMAKE_PROJECT_NAME}
        SheriffOS/src/core.cpp
        App/motor_demo.cpp
        App/dr16.cpp
)

# 添加源文件
file(GLOB_RECURSE source_c CONFIGURE_DEPENDS
        App/*.cpp App/*.c App/*.h
)
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
        ${source_c}
)

# 添加头文件目录
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        App/include
        App
)

# ---- 添加静态库
# ---- SheriffOS
add_subdirectory(SheriffOS)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE os)
# ---- BSP
add_subdirectory(BSP)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE bsp)
# ---- Device
#add_subdirectory(Device)
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE device)


# 生成bin和hex
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O ihex ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.hex
        COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
        COMMENT "Generating HEX and BIN files"
)

